package mysgoog.util;

import java.io.*;
import java.util.*;
/**
* @author Fu, Zhongyang
*/
public class SGFReader
{
    List<String> pattern = new ArrayList<String>();
    String s = "";
    int level = 0;
    char lastchar1 = '1';
    char lastchar2 = '1';
    String answer = "";
    int hasStone[] = new int[1000];
    int count = 0;

    public static void main(String arg[])
    {
        new SGFReader("pattern\\mokuhazushi.sgf");
    }

    public SGFReader(String filename)
    {
        for (int i = 0; i < hasStone.length; i++)
            hasStone[i] = 0;
        read(filename);
        FileManager.write("pattern\\mokuhazushi.pattern", s);
    }

    public void read(String filename)
    {
        BufferedReader reader = null;
        try
        {
            File file = new File(filename);
            reader = new BufferedReader(new FileReader(file));
            String text = null;
            while ((text = reader.readLine()) != null)
            {
                // System.err.println(text);
                parse(text);
            }
        } catch (FileNotFoundException e)
        {
            e.printStackTrace();
        } catch (IOException e)
        {
            e.printStackTrace();
        } finally
        {
            try
            {
                if (reader != null)
                {
                    reader.close();
                }
            } catch (IOException e)
            {
                e.printStackTrace();
            }
        }
    }

    public void parse(String content)
    {
        for (int i = 0; i < content.length(); i++)
        {
            // System.out.println(i + " " + level);
            char tmp = content.charAt(i);
            if (count > 0)
            {
                answer += tmp;
                count--;
                if (count == 0)
                {
                    answer += " ";
                    pattern.add(answer);
                    s += answer + "\n";
                    // System.out.println(answer);
                    System.err.println(pattern.size());
                }
            }
            if (tmp == '(')
                level++;
            if (tmp == ')')
            {
                answer = answer.substring(0, answer.length() - 4 * hasStone[level]);
                hasStone[level] = 0;
                level--;
            }
            if (tmp == '[')
            {
                if ((lastchar1 == 'B') || (lastchar1 == 'W'))
                    if (!(((lastchar2 < 'Z') && (lastchar2 > 'A')) || ((lastchar2 < 'z') && (lastchar2 > 'a'))))
                    {
                        answer += lastchar1;
                        count = 2;
                        hasStone[level]++;
                        // System.out.println(level + "true");
                    }
            }
            lastchar2 = lastchar1;
            lastchar1 = tmp;
        }
    }
}
